<template>
  <el-dialog title="编辑采购单详情" :visible.sync="dialogVisible" width="1500px"
    :close-on-click-modal="false" :modal-append-to-body="false" v-drag2anywhere>
   <el-form :model="formData" ref="formData" label-width="100px" :rules="rules" size="mini" >
     <el-row :gutter="20">
       <el-col style="width: 300px;">
         <el-form-item label="采购日期" prop="reqdate">
           <el-date-picker value-format="yyyy-MM-dd HH:mm:ss" type="datetime" style="width: 100%;" v-model="formData.reqdate" placeholder="采购日期"></el-date-picker>
         </el-form-item>
       </el-col>
       <el-col  style="width: 450px; margin-left: 50px; margin-right: 30px;">
         <el-form-item label="销方单位:" prop="seller">
           <el-row>
             <el-col :span="19">
               <el-select v-model="formData.seller" style="width: 100%;" filterable @change="handleSellerChanged">
                 <el-option v-for="(item,index) in sellerlist" :label="item.title" :value="item.title" :key="index">
                 </el-option>
               </el-select>
             </el-col>
             <el-col :span="4">
               <el-button type="primary" icon="el-icon-plus" @click="handleCreateSup">新增采购供应商</el-button>
             </el-col>
           </el-row>
         </el-form-item>
       </el-col>
       <el-col style="width:600px">
         <el-form-item label="银行账号:"><span style="color:#a2a2a2">{{formData.bankname}}-{{formData.banknumber}} <a
               href="../"></a></span></el-form-item>
       </el-col>
     </el-row>
     <el-row :gutter="20">
       <el-col :span="4">
         <el-form-item label="采购途径:" prop="buyroute">
           <el-select allow-create v-model="formData.buyroute" filterable @change="handleRoutechagned" clearable >
             <el-option label="线下采购" value="线下采购"></el-option>
             <el-option label="京东自营" value="京东自营"></el-option>
             <el-option label="工品优选" value="工品优选"></el-option>
             <el-option label="天猫" value="天猫"></el-option>
             <el-option label="1688" value="1688"></el-option>
             <el-option label="拼多多" value="拼多多"></el-option>
             <el-option label="淘宝" value="淘宝"></el-option>
             <el-option label="其他" value="其他"></el-option>
           </el-select>
         </el-form-item>
       </el-col>
       <el-col :span="4">
         <el-form-item label="付款方式:" prop="paytype">
           <el-select allow-create v-model="formData.paytype" filterable @change="handlepaytypeChanged" clearable :disabled="formData.paidstatus==1">
             <el-option label="对公转账" value="对公转账"></el-option>
             <el-option label="支付宝" value="支付宝"></el-option>
             <el-option label="微信" value="微信"></el-option>
             <el-option label="1688诚意赊" value="1688诚意赊"></el-option>
             <el-option label="京东金采" value="京东金采"></el-option>
             <el-option label="现金" value="现金支付"></el-option>
             <el-option label="其他" value="其他"></el-option>
           </el-select>
         </el-form-item>
       </el-col>
       <el-col :span="4">
         <el-form-item label="发票类型:" prop="tickettype">
           <el-select v-model="formData.tickettype">
             <el-option label="无票" value="无票"></el-option>
             <el-option label="普通发票" value="普通发票"></el-option>
             <el-option label="增值税专票" value="增值税专票"></el-option>
             <el-option label="累计普票" value="累计普票"></el-option>
             <el-option label="累计专票" value="累计专票"></el-option>
           </el-select>
         </el-form-item>
       </el-col>
       <el-col :span="4">
         <el-form-item label="运费情况:" prop="yunfei">
           <el-select v-model="formData.yunfei" placeholder="运费情况">
            <el-option label="不包邮" value="不包邮"></el-option>
            <el-option label="自提发货" value="自提发货"></el-option>
            <el-option label="包邮自提" value="包邮自提"></el-option>
            <el-option label="送货上门" value="送货上门"></el-option>
           </el-select>
         </el-form-item>
       </el-col>
       <el-col :span="8">
         <el-form-item label="店铺名称:" prop="shopname">
           <el-input v-model="formData.shopname" auto-complete placeholder="填写店铺名称"></el-input>
         </el-form-item>
       </el-col>
     </el-row>
     <el-row :gutter="20">
       <el-col style="width: 250px;">
         <el-form-item label="客户" prop="client">
           <el-autocomplete v-model="formData.client" placeholder="客户" :fetch-suggestions="querySearchAsync"  @select="handleClientSelected"  style="width: 100%;" clearable>
           </el-autocomplete>
         </el-form-item>
       </el-col>
       <el-col style="width:490px;">
         <el-form-item label="客户单位:" prop="clientname">
           <el-input v-model="formData.clientname" placeholder="客户单位" style="width: 100%;" clearable>  </el-input>
         </el-form-item>
       </el-col>
       <el-col :span="4">
         <el-form-item label="业务员:" prop="requester">
           <el-select v-model="formData.requester" placeholder="业务员">
             <el-option v-for="(item,index) in salelist" :label="item.name" :value="item.name"
               :key="item.id"></el-option>
           </el-select>
         </el-form-item>
       </el-col>

       <el-col :span="8">
         <el-form-item label="店铺链接:" prop="shopurl">
           <el-input v-model="formData.shopurl" placeholder="填写店铺链接"></el-input>
         </el-form-item>
       </el-col>
     </el-row>
     <el-row :gutter="20">
       <el-col :span="12">
         <el-form-item label="收货信息">
           <el-select v-model="formData.receiverinfo" placeholder="收货信息" style="width: 100%;" allow-create filterable clearable>
               <el-option v-for="(item,index) in addressmore" :key="item.address" :label="item.username+' '+item.mobile+' '+item.address" :value="item.username+' '+item.mobile+' '+item.address"></el-option>
           </el-select>
         </el-form-item>
       </el-col>
       <el-col :span="8">
         <el-form-item label="所属账套:" prop="buyerid">
           <el-select v-model="formData.buyerid" style="width: 100%;" @change="handleBuyerChanged">
             <el-option v-for="(item,index) in buyerlist" :label="item.name" :value="item.id" :key="index"></el-option>
           </el-select>
         </el-form-item>
       </el-col>

     </el-row>
     <el-row :gutter="20">
       <el-col>
         <el-form-item label="采购明细:">
           <div style="display: flex;">
             <div style="flex: 1;">提示: 带 <span style="color: #FF0000;">*</span> 项为必填项</div>
             <div>
               <el-button-group  v-if="formData.paidstatus==0">
                 <el-button type="primary" icon="el-icon-finished" size="mini"
                   @click="handleFilterAdd">选择录入</el-button>
                 <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">手动录入</el-button>
                 <el-upload style="display: flex;" ref="importUpload" accept=".xls,.xlsx" action=""
                   :on-change="handleImportXls" :show-file-list="false" :auto-upload="false">
                   <el-button slot="trigger" type="primary" icon="el-icon-upload2" size="mini">导入XLSX</el-button>
                   <i class="el-icon-question"
                     style="line-height: 30px;font-size: 20px; margin: 0px 5px; color: #ff5500;" title="点击下载导入模板"
                     @click="downloadTemp"></i>
                 </el-upload>
               </el-button-group>
               <el-button type="primary" icon="el-icon-download" @click="handleExportList">导出明细</el-button>
             </div>
           </div>
           <div style="height:260px; overflow-y: auto; border: 1px solid #EAEAEA;">
             <el-table :data="formData.goods" border>
               <el-table-column label="序号" type="index" align="center" width="50px"></el-table-column>
               <el-table-column label="商品名称" width="240px">
                 <template slot="header">
                   <span style="color: #FF0000;">*</span> 商品名称
                 </template>
                 <template v-slot="scope">
                   <el-input v-model="scope.row.title" placeholder="商品名称"></el-input>
                 </template>
               </el-table-column>
               <el-table-column label="品牌" >
                 <template v-slot="scope">
                   <el-input v-model="scope.row.brand" placeholder="品牌" ></el-input>
                 </template>
               </el-table-column>
               <el-table-column label="规格型号">
                 <template v-slot="scope">
                   <el-input v-model="scope.row.model" placeholder="规格型号" ></el-input>
                 </template>
               </el-table-column>
               <el-table-column label="单位" align="center">
                 <template slot="header">
                   <span style="color: #FF0000;">*</span> 单位
                 </template>
                 <template v-slot="scope">
                   <el-input v-model="scope.row.unit" placeholder="单位" ></el-input>
                 </template>
               </el-table-column>
               <el-table-column label="数量" align="center">
                 <template slot="header">
                   <span style="color: #FF0000;">*</span> 数量
                 </template>
                 <template v-slot="scope">
                   <el-input v-model="scope.row.count" @change="handleSum" ></el-input>
                 </template>
               </el-table-column>
               <el-table-column label="单价" align="center">
                 <template slot="header">
                   <span style="color: #FF0000;">*</span> 单价
                 </template>
                 <template v-slot="scope">
                   <el-input v-model="scope.row.price" @change="handleSum" ></el-input>
                 </template>
               </el-table-column>
               <el-table-column label="小计" align="center" prop="total">
                 <template v-slot="scope">
                   {{DNumber(scope.row.total,4)}}
                 </template>
               </el-table-column>
               <el-table-column label="备注" align="center">
                 <template v-slot="scope">
                   <el-input v-model="scope.row.dmemo" placeholder="备注信息" ></el-input>
                 </template>
               </el-table-column>
               <el-table-column label="操作" align="center" width="80px" v-if="formData.paidstatus==0">
                 <template v-slot="scope">
                   <el-button circle icon="el-icon-delete" type="danger" @click="handleDelete(scope.$index,scope.row)">
                   </el-button>
                 </template>
               </el-table-column>
             </el-table>
           </div>
           <div>
             <div style="float: right; margin:0px 5px 0px 0px;">总价:{{formData.sum}}</div>
           </div>
         </el-form-item>
       </el-col>
     </el-row>
     <el-row>
       <el-col :span="12">
         <el-form-item label="购买凭证:">
           <div style="margin:0px;">需要上传采购合同(双方公章)，收据等购买凭证。</div>
           <el-upload list-type="picture-card" ref="upload" :file-list="upconfig.imgList" :action="upconfig.action"
             :headers="upconfig.headers" :accept="upconfig.accept" :limit="upconfig.limit" :on-change="handleChange"
             :auto-upload="false" :on-remove="handleRemove" :on-exceed="handleLimit" multiple
             :on-preview="handlePreview">
             <i class="el-icon-plus"></i>
           </el-upload>
         </el-form-item>
       </el-col>
       <el-col :span="12">
         <el-form-item label="备注" prop="memo">
           <el-input type="textarea" :rows="5" v-model="formData.memo" maxlength="255" show-word-limit
             placeholder="备注信息"></el-input>
         </el-form-item>
       </el-col>
     </el-row>
   </el-form>
    <AttachViewer ref="attachUI" v-if="attachVisible"></AttachViewer>
    <SupCreateUI v-if="supcreateVisible" ref="supCreateUI" @closeHandler="handleSupCreateCallback"></SupCreateUI>
    <BuyFilterUI v-if="filterVisible" ref="buyfilterUI" @completeHandler="handleFilterAddCallback"></BuyFilterUI>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false" size="medium">取 消</el-button>
      <el-button type="primary" size="medium" @click="handleSave" :disabled="formData.paidstatus=='1'">保存修改</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import SupCreateUI from './sup_create.vue'
  import BuyFilterUI from './buydetailfilter.vue';
  import AttachViewer from '@/components/attachviewer.vue';
  export default {
    components: {
      SupCreateUI,
      BuyFilterUI,
      AttachViewer
    },
    data() {
      return {
        index: -1,
        attachVisible:false,
        formData: {
          reqdate:'',
          id: '',
          buyerid:'',
          reqcode:'',
          buyer:'',
          seller: '',
          buyroute: '',
          shopname: '',
          shopurl: '',
          buytype: '线上采购',
          paytype: '对公转账',
          tickettype: '增值税专用发票',
          yunfei: '',
          sum: 0,
          client: '',
          clientname:'',
          goods: [],
          memo: '',
          requester: '',
        },
        addressmore:[],
        buyerlist: [],
        salelist: [],
        sellerlist: [],
        dialogVisible: false,
        supcreateVisible: false,
        filterVisible: false,
        //文件上传相关变量
        upconfig: {
          //文件上传相关参数
          accept: '.jpg,.jpeg,.png,.pdf',
          limit: 5,
          headers: {
            token: localStorage.getItem('token') != null ? localStorage.getItem('token') : ''
          },
          imgList: [],
          fileList: [],
          rids: [], //要删除的id
          action: ""
        },
        rules: {
          supname: [{
            required: true,
            message: '供应商不能为空!',
            trigger: 'blur'
          }],
          client: [{
            required: true,
            message: '请填写客户信息',
            trigger: 'blur'
          }],
          requester: [{
            required: true,
            message: '请填写付款申请人',
            trigger: 'blur'
          }],
          seller: [{
            required: true,
            message: '销方单位不能为空',
            trigger: 'change'
          }],
          tickettype: [{
            required: true,
            message: '开票类型不能为空',
            trigger: 'blur'
          }],
          buyroute: [{
            required: true,
            message: '线上采购时，采购途径为必填项!',
            trigger: 'blur'
          }],

          ticketrate: [{
            required: true,
            message: '请选择发票税率',
            trigger: 'change'
          }],
          client: [{
            required: true,
            message: '请填写客户信息',
            trigger: 'blur'
          }],
          yunfei: [{
            required: true,
            message: '请选择运费情况',
            trigger: 'change'
          }]
        }
      }
    },
    mounted() {
      this.loadSaleList();
    },
    methods: {
      handleClientSelected(v){
       this.formData.address="";
        this.addressmore=[];
        //获取用户所有地址
        this.axios.post("api/admin/clientuser/list_by_name",{username:v.username}).then(res => {
           if(res.status==200 && res.data.code==1){
             this.addressmore=res.data.addressmore;
           }

        });
       this.formData.clientname=v.company;
      },
      handleExportList(){
        var index=1;
         let slist=this.formData.goods.map((item)=>{
          let obj={
            "序号":index,
            "产品名称":item.title,
            "品牌":item.brand,
            "规格型号":item.model,
            "单位":item.unit,
            "数量":item.count,
            "单价":item.price,
            "总价":item.total,
            "备注":item.dmemo,
            "所属账套":this.formData.buyer,
            "客户单位":this.formData.clientname
          }
          index++;
          return obj;
        });
        let ws= XLSX.utils.json_to_sheet(slist,{skipHeader:false});
           ws["!cols"]=[{wpx:80}];
           let wb=XLSX.utils.book_new();
           XLSX.utils.book_append_sheet(wb,ws,'采购明细');
           XLSX.writeFile(wb,"采购明细-"+this.formData.reqcode+".xlsx");
      },
      handleImportXls(file, filelist) {
        var _this = this;
        console.log(file.raw);
        var reader = new FileReader();
        reader.onload = function(e) {
          var data = e.target.result;
          var workbook = XLSX.read(data, {
            type: 'binary',
            cellStyles: true
          });
          //转json
          var rows = XLSX.utils.sheet_to_json(workbook.Sheets[workbook.SheetNames[0]], {
            header: 1,
            range: 1
          });
          _this.formData.goods = [];
          rows.forEach((row) => {
            _this.formData.goods.push({
              did: -1,
              title: row[2],
              brand: row[1],
              model: row[3],
              unit: row[4],
              count: row[5],
              price: row[6],
              total: row[7],
              dmemo: row[8]
            });
          })
        }
        reader.readAsBinaryString(file.raw);
      },
      downloadTemp() {
        this.tableData = [{
          index: 1,
          brand:'品牌',
          title: "商品1",
          model: "A100",
          unit: "个",
          price: 10,
          count: 1,
          total: 10,
          dmemo: '无'
        }];
        let slist = this.tableData.map((item) => {
          let obj = {
            "序号": item.index,
            "品牌":item.brand,
            "商品名称": item.title,
            "规格型号": item.model,
            "单位": item.unit,
            "数量": item.count,
            "单价": item.price,
            "总价": item.total,
            "备注": item.dmemo,
          }
          return obj;
        })
        let ws = XLSX.utils.json_to_sheet(slist, {
          skipHeader: false
        });
        ws["!cols"] = [{
          wpx: 100
        }];
        let wb = XLSX.utils.book_new();
        XLSX.utils.book_append_sheet(wb, ws, '明细');
        XLSX.writeFile(wb, "采购明细导入模板.xlsx");
      },
      handleRoutechagned(val){
        if(val.indexOf("京东")!=-1){
          this.formData.tickettype="增值税专票";
          this.formData.ticketrate="13%";
          this.formData.paytype="京东金采";
          this.formData.yunfei="含运费(送货上门)";
          this.$message.info({message:"根据采购途已同步付款方式发票类型等信息，请仔细核对!"});
        }else if(val.indexOf("天猫")!=-1){
          this.formData.tickettype="普通发票";
            this.formData.ticketrate="1%";
            this.formData.paytype="支付宝";
            this.formData.yunfei="含运费(送货上门)";
            this.$message.info({message:"根据采购途已同步付款方式发票类型等信息，请仔细核对!"});
        }
      },
      querySearchAsync(str,cb){
          this.axios.post("api/admin/clientuser/filter",{username:str}).then(res=>{
            if(res.data.code){
              cb(res.data.data);
            }
          })
      },
      handleFilterAdd() {
        this.filterVisible = true;
        this.$nextTick(() => {
          this.$refs.buyfilterUI.dialogVisible = true;
          this.$refs.buyfilterUI.initData();
        })
      },
      handleFilterAddCallback(list) {
        list.forEach(item => {
          var ngood = {
            did: -1,
            title: item.title,
            brand: item.brand,
            model: item.model,
            supid: this.formData.supid,
            unit: item.unit,
            price: item.price,
            count: 1,
            total: item.price,
            dmemo: '',
          }
          this.formData.goods.push(ngood);
        });
        this.handleSum();
      },
      loadSaleList() {
        this.axios.post("api/admin/mate/list_op").then(res => {
          if (res.status == 200 && res.data.code == 0) {
            this.salelist = res.data.list;
          }
        })
      },
      loadSuplist() { //获取合作商列表
        this.axios.post("api/admin/supplier/list").then(res => {
          if (res.status == 200 && res.data.code == 0) {
            this.suppliers = res.data.list;
            //选中默认sup
            /*var userinfo=JSON.parse(localStorage.getItem("userinfo"))
            this.formData.supid=userinfo.supid+"";*/
          }
        });
      },
      handleCkSupid(val) {
        var option = this.suppliers.filter(function(cur, index, a) {
          if (cur.id == val) {
            return cur;
          }
        })[0];
        this.formData.supid = val;
        this.formData.cksupname = option.name;
      },
      handleCreateSup() {
        this.supcreateVisible = true;
        this.$nextTick(() => {
          this.$refs.supCreateUI.dialogVisible = true;
        })
      },
      handleSupCreateCallback() {
        this.loadSellerList();
      },
      /***********************文件上传区域开始****************/
      //文件数量超限提示
      handleLimit: function() {
        this.$message.warning("最多可上传" + this.upconfig.limit + "个文件");
      },
      handleChange: function(file, list) {
        //判断文件格式和数量
        var ext = file.name.substr(file.name.lastIndexOf('.') + 1).toLowerCase();
        var extarr = ['jpg', 'jpeg', 'png', "pdf"];
        var sizeok = file.size / 1024 / 1024 < 5;
        if (extarr.lastIndexOf(ext) == -1) {
          this.$message.error("上传文件格式必须为" + extarr.join(","));
          //删除刚上传的文件
          this.upconfig.fileList.splice(this.upconfig.fileList.length - 1, 1);
          return;
        }
        if (!sizeok) {
          this.$message.error("上传文件大小最大不超过5M");
          this.upconfig.fileList.splice(this.upconfig.fileList.length - 1, 1);
          return;
        }

        this.upconfig.fileList = list;
      },
      // 文件列表移除文件时的钩子
      handleRemove: function(file, fileList) {
        if (file.id) { //如果存储 id即为已经上传好的图片
          this.upconfig.rids.push(file.id);
        }
        this.upconfig.fileList = fileList;
      },
      handlepaytypeChanged(type) {
        if (type == '对公转账') {
          this.rules.bankname = [{
            required: true,
            message: '对公转账时，开户行名称不能为空！',
            blur: 'change'
          }];
          this.rules.banknumber = [{
            required: true,
            message: '对公转账时，银行账号不能为空！',
            blur: 'change'
          }];
        } else {
          this.rules.bankname = [];
          this.rules.banknumber = [];
        }
      },
      format2Number(value, len) {
        var f = parseFloat(value);
        var f = Math.round(value * 10000) / 10000;
        var s = f.toString();
        var rs = s.indexOf('.');
        if (rs < 0) {
          rs = s.length;
          s += '.';
        }
        while (s.length <= rs + len) {
          s += '0';
        }
        return s;
      },
      handleSum() {
        this.formData.sum = 0.0000;
        this.formData.goods.forEach(item => {
          item.total = item.price * parseFloat(item.count);
          this.formData.sum += item.total;
        })
        this.formData.sum = this.format2Number(this.formData.sum, 4);
      },
      initData(index, data) {
        this.index = index;
        this.loadBuyerList();
        this.loadSellerList();
        this.formData = data;
        this.upconfig.imgList = data.buy_attach;
        this.handleSum();
      },
      loadSellerList() {
        this.axios.post("api/admin/buysupplier/list_item").then(res => {
          if (res.status == 200 && res.data.code == 0) {
            this.sellerlist = res.data.list;
          }
        });
      },
      handlePreview(file) {
          this.attachVisible=true;
          this.$nextTick(()=>{
             this.$refs.attachUI.dialogVisible=true;
             this.$refs.attachUI.initData(this.formData.buy_attach);
          })
      },
      handleSave() {
        this.$refs.formData.validate(ok => {
          if (ok) {
            //检测物品信息
            let index = 1;
            let verifyOk = true;
            for (var i = 0; i < this.formData.goods.length; i++) {
              index = (i + 1);
              if (this.formData.goods[i].title == "") {
                this.$message.error({
                  message: '第' + index + "行 商品名称填写不正确!",
                  duration: 1500,
                  showClose: true
                });
                verifyOk = false;
                break;
              } else if (this.formData.goods[i].unit == "") {
                this.$message.error({
                  message: '第' + index + "行 计量单位填写不正确!",
                  duration: 1500,
                  showClose: true
                });
                verifyOk = false;
                break;
              } else if (this.formData.goods[i].count == "" || this.formData.goods[i].count == "") {
                this.$message.error({
                  message: '第' + index + "行 采购数量填写不正确!",
                  duration: 1500,
                  showClose: true
                });
                verifyOk = false;
                break;
              }
            }

            if (!verifyOk) {
              return;
            }

            if (this.formData.sum == 0) {
              this.$message.error({
                message: "付款申请总额不能为0.00元",
                duration: 1500,
                showClose: true
              });
              return;
            }

            //提交
            var fd = new FormData();
            fd.append("id", this.formData.id);
            fd.append("rids", this.upconfig.rids);
            fd.append("reqcode",this.formData.reqcode);
             fd.append("reqdate",this.formData.reqdate);
             fd.append("sellerid",this.formData.sellerid);
             fd.append("seller", this.formData.seller);
             fd.append("buyerid",this.formData.buyerid);
             fd.append("buyer",this.formData.buyer);
             fd.append("bankname", this.formData.bankname);
             fd.append("paytype", this.formData.paytype);
             fd.append("banknumber", this.formData.banknumber);
             fd.append("shopname", this.formData.shopname);
             fd.append("buyroute", this.formData.buyroute);
             fd.append("shopurl", this.formData.shopurl);
             fd.append("goods", JSON.stringify(this.formData.goods));
             fd.append("sum", this.formData.sum);
             fd.append("buyerid", this.formData.buyerid);
             fd.append("yunfei", this.formData.yunfei);
             fd.append("client", this.formData.client);
             fd.append("clientname",this.formData.clientname);
             fd.append("tickettype", this.formData.tickettype);
             fd.append("memo", this.formData.memo);
             fd.append("receiverinfo",this.formData.receiverinfo);
             fd.append("requester", this.formData.requester);

            //附件上传
            let fid = 0;
            this.upconfig.fileList.forEach(file => {
              if (file.raw) {
                fd.append("file" + fid, file.raw);
                fid++;
              }
            });

            this.$axiosUploader.post("api/admin/buylist/update", fd).then(res => {
              if (res.status == 200 && res.data.code) {
                this.$message.success({
                  message: res.data.msg,
                  duration: 1500,
                  showClose: true
                });
                this.formData.buy_attach = res.data.attachlist;
                this.upconfig.imgList = res.data.attachlist;
                this.dialogVisible = false;
                this.$emit("completeHandler", this.index, this.formData);
                this.upconfig.fileList = [];
                this.$refs.upload.clearFiles();
              } else {
                this.$message.error({
                  message: res.data.msg,
                  duration: 1500,
                  showClose: true
                });
              }
            });

          }
        });
      },

      loadBuyerList() { //获取合作商列表
        this.axios.post("api/admin/account/list").then(res => {
          if (res.status == 200 && res.data.code == 0) {
            this.buyerlist = res.data.list;
            //选中默认sup
            /*var userinfo=JSON.parse(localStorage.getItem("userinfo"))
            this.formData.buyerid=userinfo.buyerid+"";*/
          }
        });
      },
      handleDelete(index, row) {
        this.$confirm("确定要删除此项吗?", "提示").then(() => {
          this.formData.goods.splice(index, 1);
          this.handleSum();
        })
      },
       handleAdd() {
         this.formData.goods.push({
           did: -1,
           title: '',
           brand:'',
           model: '',
           unit: '',
           count: 1,
           price: '',
           total: '',
           dmemo: '',
         });

       },
       handleSellerChanged(seller) {
        var option = this.sellerlist.filter(function(cur, index, a) {
          if (cur.title == seller) {
            return cur;
          }
        })[0];
        this.formData.bankname = option.bankname;
        this.formData.banknumber = option.banknumber;
        this.formData.sellerid=option.id;
        this.rules.bankname = [];
        this.rules.banknumber = [];
        this.rules.bankname = [{
          required: true,
          message: '对公打款时，开户行名称不能为空！',
          blur: 'change'
        }];
        this.rules.banknumber = [{
          required: true,
          message: '对公打款时，银行账号不能为空！',
          blur: 'change'
        }];
      },
      handleBuyerChanged(buyerid){
        var option = this.buyerlist.filter(function(cur, index, a) {
          if (cur.id == buyerid) {
            return cur;
          }
        })[0];
        this.formData.buyer=option.name;
        this.formData.buyerid=option.id;
      }
    }
  }
</script>

<style>
</style>
